package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.common.core.feign.ModulesSystemFeign;
import com.ruoyi.datastatistics.domain.AgegroupResult;

import com.ruoyi.datastatistics.domain.PassrateResult;
import com.ruoyi.datastatistics.mapper.PassrateMapper;
import com.ruoyi.datastatistics.service.AgegroupResultSorter;
import com.ruoyi.datastatistics.service.PassrateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PassrateServiceImpl implements PassrateService {

    @Autowired
    private PassrateMapper passrateMapper;
    @Autowired
    private HomepageServiceImpl homepageService;
    @Autowired
    public ModulesSystemFeign systemFeign;
    @Override
    public Map<String,Object> getTotalpassrate(Parameter parameter) {
        Map<String,Object> dataMap = new HashMap<>();
        parameter = systemFeign.setParameter(parameter);
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            PassrateResult passrateResult = passrateMapper.getTotalpassrate(parameter);
            if (passrateResult.getTotalallvalue() > 0){
                AgegroupResult Result = setAgegroupResult(passrateResult);
                dataMap.put("data",Result);
                dataMap.put("message",true);
                dataMap.put("parameter",parameter);
            }else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }
    /*public static double roundToTwoDecimalPlaces(double value) {
        return new BigDecimal(Double.toString(value))
                .setScale(4, RoundingMode.HALF_UP)
                .doubleValue()*100;
    }*/
    public static double roundToTwoDecimalPlaces(double value) {
        return new BigDecimal(Double.toString(value))
                .multiply(new BigDecimal("100"))  // 先乘以100
                .setScale(1, RoundingMode.HALF_UP)  // 然后保留2位小数
                .doubleValue();
    }
    public AgegroupResult setAgegroupResult(PassrateResult passrateResult){
        AgegroupResult Result = new AgegroupResult();
        Result.setNAME(passrateResult.getNAME());
        if (passrateResult.getTotalallvalue() > 0){
            Result.setTotalvalue(roundToTwoDecimalPlaces(passrateResult.getTotalvalue()/passrateResult.getTotalallvalue()));
        }else {
            Result.setTotalvalue(0);
        }
        if (passrateResult.getManallvalue() > 0){
            Result.setManvalue(roundToTwoDecimalPlaces(passrateResult.getManvalue()/passrateResult.getManallvalue()));
        }else {
            Result.setManvalue(0);
        }
        if (passrateResult.getWomanallvalue() > 0){
            Result.setWomanvalue(roundToTwoDecimalPlaces(passrateResult.getWomanvalue()/passrateResult.getWomanallvalue()));
        }else {
            Result.setWomanvalue(0);
        }
        return Result;
    }
    public List<AgegroupResult> setAgegroupResultList(List<PassrateResult> passrateResultlist){
        List<AgegroupResult> AgegroupResultlist = new ArrayList<AgegroupResult>();
        for (PassrateResult passrateResult : passrateResultlist){
            AgegroupResult Result = new AgegroupResult();
            Result.setNAME(passrateResult.getNAME());
            if (passrateResult.getTotalallvalue() > 0){
                Result.setTotalvalue(roundToTwoDecimalPlaces(passrateResult.getTotalvalue()/passrateResult.getTotalallvalue()));
            }else {
                Result.setTotalvalue(0);
            }
            if (passrateResult.getManallvalue() > 0){
                Result.setManvalue(roundToTwoDecimalPlaces(passrateResult.getManvalue()/passrateResult.getManallvalue()));
            }else {
                Result.setManvalue(0);
            }
            if (passrateResult.getWomanallvalue() > 0){
                Result.setWomanvalue(roundToTwoDecimalPlaces(passrateResult.getWomanvalue()/passrateResult.getWomanallvalue()));
            }else {
                Result.setWomanvalue(0);
            }
            AgegroupResultlist.add(Result);
        }
            return AgegroupResultlist;
    }
    @Override
    public Map<String,Object> getAgegrouppassrate(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<PassrateResult> passrateResultList = passrateMapper.getAgegrouppassrate(parameter);
            if (passrateResultList.size() > 0){
                List<AgegroupResult> Result = setAgegroupResultList(passrateResultList);
                // 定义排序顺序
                List<String> customOrder  = Arrays.asList("幼儿", "成年","老年");
                // 按照自定义顺序排序
                List<AgegroupResult> sortedResults = AgegroupResultSorter.sortByCustomOrder(
                        Result,
                        AgegroupResult::getNAME,
                        customOrder
                );
                dataMap.put("data",sortedResults);
                dataMap.put("message",true);
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getAgebracketpassrate(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<PassrateResult> passrateResultList = passrateMapper.getAgebracketpassrate(parameter);
            if (passrateResultList.size() > 0){
                List<AgegroupResult> Result = setAgegroupResultList(passrateResultList);
                // 定义排序顺序
                List<String> customOrder  = Arrays.asList("3岁", "3.5岁","4岁", "4.5岁","5岁", "5.5岁","6岁","6.5岁", "20岁~24岁","25岁~29岁", "30岁~34岁","35岁~39岁", "40岁~44岁","45岁~49岁", "50岁~54岁","55岁~59岁", "60岁~64岁","65岁~69岁", "70岁~74岁","75岁~79岁");
                // 按照自定义顺序排序
                List<AgegroupResult> sortedResults = AgegroupResultSorter.sortByCustomOrder(
                        Result,
                        AgegroupResult::getNAME,
                        customOrder
                );
                dataMap.put("data",sortedResults);
                dataMap.put("message",true);
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getUrbanandruralpassrate(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<PassrateResult> passrateResultList = passrateMapper.getUrbanandruralpassrate(parameter);
            if (passrateResultList.size() > 0){
                List<AgegroupResult> Result = setAgegroupResultList(passrateResultList);
                // 定义排序顺序
                List<String> customOrder  = Arrays.asList("城镇幼儿", "农村幼儿", "城镇非体力劳动者", "城镇体力劳动者", "农民", "城镇老年人", "农村老年人");
                // 按照自定义顺序排序
                List<AgegroupResult> sortedResults = AgegroupResultSorter.sortByCustomOrder(
                        Result,
                        AgegroupResult::getNAME,
                        customOrder
                );
                dataMap.put("data",sortedResults);
                dataMap.put("message",true);
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getMonthpassrate(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<PassrateResult> passrateResultList = passrateMapper.getMonthpassrate(parameter);
            if (passrateResultList.size() > 0){
                List<AgegroupResult> Result = setAgegroupResultList(passrateResultList);
                List<AgegroupResult> sortedList = Result.stream()
                        .sorted((a1, a2) -> {
                            try {
                                int num1 = Integer.parseInt(a1.getNAME());
                                int num2 = Integer.parseInt(a2.getNAME());
                                return Integer.compare(num1, num2);
                            } catch (NumberFormatException e) {
                                // 处理非数字情况，例如按字符串排序或赋予默认值
                                return a1.getNAME().compareTo(a2.getNAME());
                            }
                        })
                        .collect(Collectors.toList());
                dataMap.put("data",sortedList);
                dataMap.put("message",true);
            } else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }
        return dataMap;
    }

    @Override
    public Map<String,Object> getNextdeptpassrate(Parameter parameter) {
        parameter = systemFeign.setParameter(parameter);
        Map<String,Object> dataMap = new HashMap<>();
        if (parameter.getOrganizationCode() != null && !parameter.getOrganizationCode().equals("")){
            List<PassrateResult> passrateResultList = passrateMapper.getNextdeptpassrate(parameter);
            if (passrateResultList.size() > 0){
                List<AgegroupResult> Result = setAgegroupResultList(passrateResultList);
                dataMap.put("data",Result);
                dataMap.put("message",true);
            }else {
                dataMap.put("data",null);
                dataMap.put("message","请在机构管理中添加该机构信息");
            }
        }else {
            dataMap.put("data",null);
            dataMap.put("message","请在机构管理中添加该机构信息");

        }
        return dataMap;
    }
}
